package com.xwzn.resident;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

/**
 * @Description 自定义ToastView
 * @Author HanKaiYang
 * @CreateDate 2019/12/23 10:21
 * @Version 1.0
 */
public class BaseToastView {
    private static boolean TOAST_TF = true;
    private static boolean LOG_IF = true;

    private static Toast toast;
    private View view;
    private Context context;

    public BaseToastView() {
    }

    public BaseToastView(Context context) {
        this.context = context;
    }

    public void setContext(Context context){
        this.context = context;
    }

    public Toast getToast() {
        return toast;
    }

    public void show() {
        toast.show();
    }

    public View getView() {
        return view;
    }


    /**
     * @author HanKaiYang
     * @createDate 2019/12/23 10:19
     * @param layoutResId 布局id
     * @return null
     */
    public BaseToastView initLayout(int layoutResId){
        if (TOAST_TF){
            if (context != null){
                view = LayoutInflater.from(context).inflate(layoutResId,null);
                toast = new Toast(context);
                toast.setView(view);
            }else {
                if (LOG_IF){
                    Log.d("BaseToast","消息框错误日志：你没有为 Context 进行赋值 ，却引用了 Toast 导致该功能无法实现。请初始化时加入Content参数或使用setContent()方法");
                }
            }
        }
        return this;
    }

    /**
     * @author HanKaiYang
     * @createDate 2019/12/23 10:19
     * @param layoutResId  布局
     * @param gravity Gravity.*****  用这个变量里面的值可以控制显示位置 如果为 0 就显示默认位置
     * @return null
     */
    public BaseToastView initLayout(int layoutResId, int gravity) {
        if (TOAST_TF){
            if (context != null){
                view = LayoutInflater.from(context).inflate(layoutResId,null);
                toast = new Toast(context);
                if (gravity != 0){
                    toast.setGravity(gravity,0,0);
                }
                toast.setView(view);
            }else {
                if (LOG_IF){
                    Log.d("BaseToast","消息框错误日志：你没有为 Context 进行赋值 ，却引用了 Toast 导致该功能无法实现。请初始化时加入Content参数或使用setContent()方法");
                }
            }
        }
        return this;
    }

    /**
     * @author HanKaiYang
     * @createDate 2019/12/23 10:19
     * @param layoutResId  布局
     * @param gravity Gravity.*****  用这个变量里面的值可以控制显示位置 如果为 0
     * @param context context
     * @return null
     */
    public  BaseToastView initLayout(int layoutResId, int gravity,Context context) {
        if (TOAST_TF){
            if (context != null){
                view = LayoutInflater.from(context).inflate(layoutResId,null);
                toast = new Toast(context);
                if (gravity != 0){
                    toast.setGravity(gravity,0,0);
                }
                toast.setView(view);
            }else {
                if (LOG_IF){
                    Log.d("BaseToast","消息框错误日志：你没有为 Context 进行赋值 ，却引用了 Toast 导致该功能无法实现。请初始化时加入Content参数或调用setContent()方法");
                }
            }
        }
        return this;
    }
}
